//
//  visitor.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/6.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
import SnapKit
class visitor: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    /// 创建代理


/// 创建图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
/// 加载遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
/// 加载小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
/// 加载注册label
    private lazy var messageLabel: UILabel = UILabel(text1: "关注一些人，回这里看看有什么惊喜")
    /// 创建注册按钮
    lazy var registerBtn: UIButton = UIButton(title: "注册", backgroundImage: "common_button_white_disable", color: UIColor.orangeColor())
    /// 创建登陆按钮
     lazy var loginBtn: UIButton = UIButton(title: "登陆", backgroundImage: "common_button_white_disable", color: UIColor.darkGrayColor())
/// 设置动画
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue=2*M_PI
        anim.duration=20
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
        let anim2 = CABasicAnimation(keyPath: "transform.scale")
        anim2.toValue = 1.5
        anim2.duration = 2
        anim2.repeatCount = MAXFLOAT
        anim2.autoreverses = true
        anim2.removedOnCompletion = false
        iconView.layer .addAnimation(anim2, forKey: nil)
    }
    func setupInfo(message: String, imageName :String? ){
         messageLabel.text = message
        guard let  imgName = imageName else{
            startAnimation()
            return
        }
        iconView.image = UIImage(named: imgName)
        houseView.hidden = true
        sendSubviewToBack(houseView)
        
    }
    func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
/// 取消所有子控件 Autoresizing
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
/// 添加图片约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
/// 添加小房子的约束
        houseView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
/// 信息文字的约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
//注册按钮的约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
//登陆按钮的约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
//遮罩的约束
       maskIconView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(self.snp_top)
        make.bottom.equalTo(self.snp_bottom)
        make.left.equalTo(self.snp_left)
        make.right.equalTo(self.snp_right).offset(-36)
        }
        
       backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }

}
